<?php
/**
 * Ajax请求控制器
 */
namespace Home\Controller;
use Think\Controller;
class AjaxController extends Controller {

        //登录接口
        public function login(){
            header('Access-Control-Allow-Origin:*');
            $user_name = $_POST['user_name'];
            $password = $_POST['password'];
            $model_user = M('user');
            $user_info = $model_user->where('name = "'.$user_name.'" and password = "' . $password .'"')->select();
            //echo $model_user->getLastSql();
            if($user_info){
                $data_info['data'] = $user_info[0];
                $data_info['msg'] = "success";
                $data_info['status'] = 1;
            }else{
                $data_info['msg'] = "fail";
                $data_info['status'] = 0;
            }
            $this->ajaxReturn($data_info);exit;
        }

        //信息提交接口
        public function postInfo(){
            header('Access-Control-Allow-Origin:*');
            $model_info = M('info');
            $data = $model_info->create();
            $data['time'] = date("Y-m-d H:i:s");
            $user_id = $_POST['user_id'];
            $user = M('user')->where('id ='.$user_id)->find();
            $data['user'] = $user['name'];
            // $data['name'] = $_POST['name'];
            // $data['tel'] = $_POST['tel'];
            // $data['sex'] = $_POST['sex'];
            // $data['from'] = $_POST['from'];
            // $data['product'] = $_POST['product'];
            // $data['work'] = $_POST['work'];
            // $data['use'] = $_POST['use'];
            // $data['weixin'] = $_POST['weixin'];
            // $data['house'] = $_POST['house'];
            // $data['car'] = $_POST['car'];
            // $data['policy'] = $_POST['policy'];
            // $data['social_security'] = $_POST['social_security'];
            // $data['license'] = $_POST['license'];
            // $data['warter'] = $_POST['warter'];
            //print_r($data);
            $lastInsId = $model_info->add($data);
            if($lastInsId){
                $data_info['msg'] = "success";
                $data_info['status'] = 1;
            }else{
                $data_info['msg'] = "fail";
                $data_info['status'] = 0;
            }
            $this->ajaxReturn($data_info);exit;
        }

        public function upload(){
            header('Access-Control-Allow-Origin:*');
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =     ''; // 设置附件上传（子）目录
            // 上传文件
            $info   =   $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功
                $this->ajaxReturn($info);exit;
            }
        }

        //获取历史记录
        public function get_history(){
            header('Access-Control-Allow-Origin:*');
            $uid = I('uid');

            $p = I('p');
            if(!$p){
                $p = 1;
            }
            $num = 8;

            $model_info = M('info');
            if($uid!=null){
                $info = $model_info->where('user_id = '.$uid)->page($p.','.$num)->select();
                $count = $model_info->where('user_id = '.$uid)->count();
            }

            if($info){
                $data_info['msg'] = "success";
                $data_info['status'] = 1;
                $data_info['data'] = $info;
                $data_info['count'] = $count;
            }else{
                $data_info['msg'] = "fail";
                $data_info['status'] = 0;
            }
            $this->ajaxReturn($data_info);exit;
        }

        //获取单条记录
        public function get_info(){
            header('Access-Control-Allow-Origin:*');
            $id = I('id');

            $model_info = M('info');
            if($id!=null){
                $info = $model_info->where('id = '.$id)->select();
            }

            if($info){
                $data_info['msg'] = "success";
                $data_info['status'] = 1;
                $data_info['data'] = $info;
            }else{
                $data_info['msg'] = "fail";
                $data_info['status'] = 0;
            }
            $this->ajaxReturn($data_info);exit;
        }

}
